package com.qingneng.web;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;

import com.opensymphony.xwork2.Action;
import com.qingneng.service.sdk.IContractInfoService;
import com.qingneng.vo.ContractInfo;
import com.tbfs.common.BaseAction;
import com.tbfs.security.CurrentUser;
import com.tbfs.util.UtilTool;

public class ContractInfoAction extends BaseAction {

	private static final Logger logger = Logger
			.getLogger(ContractInfoAction.class);

	// init service
	public IContractInfoService contractInfoService;

	// 构造函数
	public ContractInfoAction(IContractInfoService contractInfoService) {
		this.contractInfoService = contractInfoService;
	}

	public ContractInfoAction() {

	}

	private List rows = null;

	private Integer total;

	private ContractInfo contractInfo;

	private String message;

	private String volidateAccessDataAuth;

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public ContractInfo getContractInfo() {
		return contractInfo;
	}

	public void setContractInfo(ContractInfo contractInfo) {
		this.contractInfo = contractInfo;
	}

	public List getRows() {
		return rows;
	}

	public void setRows(List rows) {
		this.rows = rows;
	}

	public Integer getTotal() {
		return total;
	}

	public void setTotal(Integer total) {
		this.total = total;
	}

	/**
	 * load contractinfo json data
	 * 
	 * @return
	 */
	public String loadData() {
		try {
			
			//初始化查询对象
			if(contractInfo==null){
				contractInfo = new ContractInfo();
			}
			//分页
			this.processPaginationBean();
			volidateAccessDataAuth = this.volidateAccessDataAuth();
			//所有数据
			if("0".equals(volidateAccessDataAuth)){
				
			}
			//公司数据数据
			if("1".equals(volidateAccessDataAuth)){
				contractInfo.setCompany_code(CurrentUser.getCurrentCompanyCode());
			}
			//个人数据
			if("2".equals(volidateAccessDataAuth)){
				contractInfo.setCreatePerson(CurrentUser.getUser().getUseraccount()); 
			}
			
			rows  =  contractInfoService.getContractInfo(contractInfo);
			total = contractInfoService.getContractInfoTotalCount(contractInfo);
			
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		return Action.SUCCESS;
	}
	
	
	/**
	 * load contractinfo json data
	 * 
	 * @return
	 */
	public String loadContractStatusData() {
		try {
			
			//初始化查询对象
			Map paramMap = new HashMap();
			
			//已编制，待审计状态
			paramMap.put("projectStatus",1);
			
			//分页
			this.processPaginationBean();
			volidateAccessDataAuth = this.volidateAccessDataAuth();
			//所有数据
			if("0".equals(volidateAccessDataAuth)){
				
			}
			//公司数据数据
			if("1".equals(volidateAccessDataAuth)){
				paramMap.put("company_code",CurrentUser.getCurrentCompanyCode());
			}
			//个人数据
			if("2".equals(volidateAccessDataAuth)){
				paramMap.put("contractPerson",CurrentUser.getUser().getUseraccount()); 
			}
			
			//根据项目已编制后，待审计状态  的方向 查找
			
			rows  =  contractInfoService.getContractInfoAduit(paramMap);
			total = new Integer(1000);
			
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		return Action.SUCCESS;
	}

	/**
	 * load contractinfo json data
	 * 
	 * @return
	 */
	public String loadMainContractInfo() {

		try {
			
			//初始化查询对象
			if(contractInfo==null){
				contractInfo = new ContractInfo();
			}
			
			//分页
			this.processPaginationBean();
			volidateAccessDataAuth = this.volidateAccessDataAuth();
			//所有数据
			if("0".equals(volidateAccessDataAuth)){
				
			}
			//公司数据数据
			if("1".equals(volidateAccessDataAuth)){
				contractInfo.setCompany_code(CurrentUser.getCurrentCompanyCode());
			}
			//个人数据
			if("2".equals(volidateAccessDataAuth)){
				contractInfo.setCreatePerson(CurrentUser.getUser().getUseraccount()); 
			}
					
			
			rows = contractInfoService.getMainContractInfo(contractInfo);
			total = contractInfoService.getContractInfoTotalCount(contractInfo);

		} catch (Exception e) {
			e.printStackTrace();
		}
		return Action.SUCCESS;
	}

	/**
	 * save contractinfo data
	 * 
	 * @return
	 */
	public String saveData() {
		if (logger.isDebugEnabled()) {
			logger.debug("+++begin save contractinfo +++");
		}
		logger.info(contractInfo.getContractName());

		try {
			contractInfo.setCreateTime(UtilTool.formatDate(new Date()));
			contractInfoService.addContractInfo(contractInfo);
			message = "success";

		} catch (Exception e) {
			message = "添加失败，请检查输入项目";
			e.printStackTrace();
		}
		if (logger.isDebugEnabled()) {
			logger.debug("+++end save contractinfo +++");
		}
		return Action.SUCCESS;
	}

}
